import os
import shutil
import threading
import logging

import requests
from playwright.sync_api import Playwright, sync_playwright, expect


def download(urls,parent_dir):
    for i in range(len(urls)):
        print('开始下载：{}',urls[i])
        try:
            r = requests.get(urls[i])
            with open(os.path.join(parent_dir,str(i)+'.png'), 'wb') as f:
                f.write(r.content)
        except Exception as ex:
            print('下载失败,',parent_dir,urls[i])
            logging.error('下载失败,%s,%s',parent_dir,urls[i])


def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://mp.weixin.qq.com/s/LeJ48LG-z9AMEqghewZRRQ")

    div = page.query_selector("#js_content")
    p_elements = div.query_selector_all('p')
    for p_element in p_elements:
        ae = p_element.query_selector('a')
        if ae:
            href = ae.get_attribute("href")
            textvalue = ae.get_attribute("textvalue")

            shutil.rmtree(textvalue,ignore_errors=True)
            os.path.join(os.getcwd(),textvalue,'001')
            os.makedirs(textvalue)
            print(textvalue)
            page1 = context.new_page()
            page1.goto(href)
            # page.click('div.album-rich_media_area_inner')
            page1.mouse.wheel(0, 1000)
            page1.mouse.wheel(0, 2000)
            page1.wait_for_timeout(2000)
            li_elements = page1.query_selector_all("div.album__content.js_album_bd > ul > li")
            for li_ele in li_elements:

                data_link = li_ele.get_attribute("data-link")
                data_title = li_ele.get_attribute("data-title")
                print(data_title)
                print(data_link)
                page2 = context.new_page()
                page2.goto(data_link)

                page2.mouse.wheel(0, 1000)
                page2.wait_for_timeout(500)
                page2.mouse.wheel(0, 2000)
                page2.wait_for_timeout(500)
                page2.mouse.wheel(0, 3000)
                page2.wait_for_timeout(500)
                page2.mouse.wheel(0, 4000)
                page2.wait_for_timeout(500)
                page2.mouse.wheel(0, 5000)
                page2.wait_for_timeout(500)

                path = os.path.join(os.getcwd(), textvalue, data_title)
                if not os.path.exists(path):
                    os.makedirs(path)
                page2.wait_for_timeout(2000)

                img_elements = page2.query_selector_all('img.rich_pages.wxw-img.js_insertlocalimg')
                img_urls = []
                for element in img_elements:
                    src = element.get_attribute("src")
                    if src.startswith("http"):
                        img_urls.append(src)
                    else:
                        src = element.get_attribute("data-src")
                        img_urls.append(src)

                threading.Thread(target=download,args=(img_urls,path)).start()  # download(img_urls,path)
                page2.wait_for_timeout(1000)
                page2.close()

            page1.wait_for_timeout(1000)
            page1.close()
    # ---------------------
    context.close()
    browser.close()






if __name__ == '__main__':
    logging.basicConfig(filename='error.log', level=logging.ERROR)
    with sync_playwright() as playwright:
        run(playwright)